package cn.forkway.utils;

import jdk.nashorn.internal.ir.Symbol;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @program: java-learn-mybatis
 * @className: MybatisUtils.java
 * @author: River Ray
 * @description: mybatis封装工具类
 * @createTime 2021年10月10日 11:16:00
 */
public class MybatisUtils {
    static SqlSessionFactory factory;

    private MybatisUtils() {
    }

    static {
        String config = "mybatis.xml";
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream(config);
            factory = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSession() {
        SqlSession sqlSession = null;
        if (factory != null) {
            sqlSession = factory.openSession();
        }
        return sqlSession;
    }

    public static SqlSession getSessions(boolean tag) {
        SqlSession sqlSession = null;
        if (factory != null) {
            sqlSession = factory.openSession(tag);
        }
        return sqlSession;
    }

}
